package com.huangshi.skinlib;

import android.graphics.ImageDecoder;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.huangshi.skinlib.utils.SkinResources;

import java.util.ArrayList;
import java.util.List;

import androidx.core.view.ViewCompat;

/**
 * 存放所有要换肤的view所对应的属性
 */
public class SkinAttribute {
    private static final List<String> mAttributes = new ArrayList<>();

    static {
        mAttributes.add("background");
        mAttributes.add("src");
        mAttributes.add("textColor");
        mAttributes.add("drawableLeft");
        mAttributes.add("drawableTop");
        mAttributes.add("drawableRight");
        mAttributes.add("drawableBottom");
    }

    // 记录换肤需要操作的View与属性信息
//    private List<SkinView>
    static class SkinView{
        View view;
        // 这个View的能被 换肤的属性与它对应的id 集合
        List<SkinPair> skinPairs;

        public SkinView(View view,List<SkinPair> skinPairs){
            this.view = view;
            this.skinPairs = skinPairs;
        }
        /**
         * 对一个View中的所有属性进行修改
         */
        public void applySkin(){
            applySkinSupport();
            for (SkinPair skinPair : skinPairs){
                Drawable left = null,top = null, right = null,bottom = null;
                switch (skinPair.attributeName){
                    case "background":
                        Object background = SkinResources.getInstance().getBackground(skinPair.resId);
                        // 背景可能是 @color 也可能是 @drawable
                        if(background instanceof Integer){
                            view.setBackgroundColor((int) background);
                        }else {
                            ViewCompat.setBackground(view,(Drawable) background);
                        }
                        break;
                    case "src":
                        background = SkinResources.getInstance().getBackground(skinPair.resId);
                        if(background instanceof Integer){
                            ((ImageView) view).setImageDrawable(new ColorDrawable((Integer)background));
                        }else {
                            ((ImageView) view).setImageDrawable((Drawable) background);
                        }
                        break;
                    case "textColor":
                        ((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList(skinPair.resId));
                        break;
                    case "drawableLeft":
                        left = SkinResources.getInstance().getDrawable(skinPair.resId);
                        break;
                    case "drawableTop":
                        top = SkinResources.getInstance().getDrawable(skinPair.resId);
                        break;
                    case "drawableRight":
                        right = SkinResources.getInstance().getDrawable(skinPair.resId);
                        break;
                }
            }
        }

        public void applySkinSupport(){
            if(view instanceof SkinViewSupport){
                ((SkinViewSupport) view).applySkin();
            }
        }
    }

    static class SkinPair{
        // 属性名
        String attributeName;
        // 对应的资源id
        int resId;

        public SkinPair(String attributeName,int resId){
            this.attributeName = attributeName;
            this.resId = resId;
        }
    }
}
